/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package query;
import graph.*;
/**
 *
 * @author Gonzalo
 */
public class Predicate{

     int bool=-1;
     String label=null;
     boolean converse=false;

     String attr=null;
     String op=null;
     String v=null;

     long node=0;

     public Predicate (boolean _bool){
        if(_bool)
            bool=1;
        else
            bool=0;
     }
     public Predicate(String _label){
         label=_label;
     }
     public Predicate(long _node){
         node=_node;
     }
     public Predicate(String _attr,String _op,String _v){
         attr=_attr;
         op=_op;
         v=_v;
     }

     public boolean isLabel(){
         if(label!=null) return true;
         return false;
     }
     public boolean isSingleton(){
         if(node!=0) return true;
         return false;
     }
     public boolean isTrue(){
         if(bool==1)
             return true;
         return false;
     }
     public boolean isFalse(){
         if(bool==0)
             return true;
         return false;
     }

     public boolean isCondition(){
         if(attr!=null) return true;
         return false;
     }

     public void flipConverse(){
         if(converse)
             converse=false;
         else
             converse=true;
         return;
     }


     public Nodes ejecutar(Graph g,Nodes N){
         if(isSingleton()){ // {n}.interseccion(N)
             if(N.contains(node))
                 return new Nodes(node);
             else
                 return new Nodes();
         }else if(isTrue()) // [A=c].interseccion(N)
            return N;
         else if(isFalse()) // [A=c].interseccion(N)
            return new Nodes();
         else if(isCondition()){ // [A=c].interseccion(N)
            return g.getNodosWhere(attr, op, v).interseccion(N);
         }else if(isLabel()){
             if(!converse){ //Normal
                return g.getTailsWithHeadsIn(label,N);
             }else{
                return g.getHeadsWithTailsIn(label,N);
             }
         }else
             return new Nodes();
     }

    public String print(){
        if(isLabel()){
            if(converse)
                return label+"^";
            else
                return label;
        }else if(isCondition())
            return "["+attr+op+v.toString()+"]";
        else if(isTrue())
            return "[T]";
        else if(isFalse())
            return "[F]";
            return "["+node+"]";
    }

}
